package tech.aistar.corejava.day15;

/**
 * @Author: lya
 * @Description: 本类的作用描述:线程之间的礼让
 * @CreateDate: 2019/8/4
 * @Version: 1.0
 */
public class YieldDemo {
    public static void main(String[] args) {
        new Y2().start();
        new Y2().start();
    }
}
class Y2 extends Thread {
    @Override
    public void run(){
        for (int i = 0; i < 10; i++) {
            if (i == 3){
                //线程之间的礼让 - 暂停当前正在执行的线程对象,并执行"其他线程"
                Thread.yield();
            }
            System.out.println(Thread.currentThread().getName()+"->"+i);

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
